import time
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction


class BasePage(object):

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"

    def __init__(self, driver=None):
        self.driver = driver

    # 启动app方法
    def lanuch_app(self):
        # desired_caps = {}
        # #平台
        # desired_caps['platformName'] = 'Android'
        # #设备版本
        # desired_caps['platformVersion'] = '9'
        # #设备名称，如果连接真机，最好填写设备名称
        # desired_caps['deviceName'] = 'c93af3c2'
        # #应用入口包名
        # desired_caps['appPackage'] = 'com.zoneyet.lovehottie'
        # # 应用入口活动名
        # desired_caps['appActivity'] = 'com.zoneyet.gaga.splash.LaunchActivity'
        # #是否开启中文键盘，设置为true可以输入中文内容
        # desired_caps['unicodeKeyboard'] = True
        # #重置键盘
        # desired_caps['resetKeyboard'] = True
        # #正常打开应用，不还原初始状态
        # desired_caps['noReset'] = True

        desired_caps = {
            "platformName": "Android",
            "platformVersion": "9",
            "deviceName": "c93af3c2",
            # "deviceName": "4f6e2287",
            "appPackage": "com.zoneyet.gaga",
            "appActivity": ".splash.LaunchActivity",
            "noSign": True,
            "automationName": "UIAutomator2",
            "noReset": True,
            "settings[waitForIdleTimeout]": 100
        }

        driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        driver.implicitly_wait('10')
        # 完成启动之后吧生成的driver赋值给self.driver
        self.driver = driver
        # 截屏方法，文件名字以当前日期拼接字符串+excel中对应sheet页名称命名
        return self.driver

    def screenshot(self, pid):
        tmp = time.strftime('%Y%m%d_%H%M%S',time.localtime(time.time()))
        self.driver.get_screenshot_as_file('./{pid}_{tmp}.png'.format(tmp=tmp, pid=pid))

    # 等待
    @staticmethod
    def waiting(s):
        time.sleep(s)

    # 向上滑动
    def swipe_up(self, t=2, count=1):
        s = self.driver.get_window_size()
        x1 = s['width'] * 0.5  # x坐标
        y1 = s['height'] * 0.75  # 起点y坐标
        y2 = s['height'] * 0.25  # 终点y坐标
        for i in range(0, count):
            self.driver.swipe(x1, y1, x1, y2, t*1000)
            self.waiting(1)

    # 向下滑动
    def swipe_down(self, t=2, count=1):
        s = self.driver.get_window_size()
        x1 = s['width'] * 0.5  # x坐标
        y1 = s['height'] * 0.25  # 起点y坐标
        y2 = s['height'] * 0.75  # 终点y坐标
        for i in range(0, count):
            self.driver.swipe(x1, y1, x1, y2, t*1000)
            self.waiting(1)

    # 向左滑动
    def swipe_left(self,  t=2, count=1):
        s = self.driver.get_window_size()
        x1 = s['width'] * 0.85
        y1 = s['height'] * 0.5
        x2 = s['width'] * 0.15
        for i in range(0, count):
            self.driver.swipe(x1, y1, x2, y1, t*1000)
            self.waiting(1)

    # 向右
    def swipe_right(self,  t=2, count=1):
        l = self.driver.get_window_size()
        x1 = l['width'] * 0.15
        y1 = l['height'] * 0.5
        x2 = l['width'] * 0.85
        for i in range(0, count):
            self.driver.swipe(x1, y1, x2, y1, t*1000)
            self.waiting(1)

    # 点击方法
    def click(self, locator):
        self.driver.find_element(locator[0], locator[1]).click()

    def send_keys(self, locator, text):  # 输入
        self.driver.find_element(locator[0], locator[1]).click()
        self.driver.find_element(locator[0], locator[1]).clear()
        self.driver.find_element(locator[0], locator[1]).send_keys(text)

    # 关闭app
    def app_closed(self):
        self.driver.quit()

    def hide_keyboard(self):
        self.driver.hide_keyboard()

    def long_press(self, locator, keeptime):
        action = TouchAction(self.driver)
        element = self.driver.find_element_by_id(locator)
        action.long_press(element).wait(keeptime).release().perform()

    def long_press_xpath(self, keeptime):
        action = TouchAction(self.driver)
        action.long_press(x=833, y=1695).wait(keeptime).perform()

    def find_element(self, locator):
        return self.driver.find_element(locator[0], locator[1])




